import openpyxl
from openpyxl.styles import Alignment, Font, PatternFill, Border, Side
from openpyxl.worksheet.page import PageMargins, PrintOptions
from calendar import monthrange
from datetime import datetime

# 常量定义
页边距 = 0.76 #厘米
年份行高 = 70  # 调整年份行高
年份字体 = 'Arial'
年份字号 = 72  # 增大年份字号
月份字体 = 'Arial'
月份字号 = 22  # 增大月份字号
日期字体 = 'Arial'
日期字号 = 14  # 增大日期字体
星期字体 = 'Arial'
星期字号 = 11
逻辑日空间第1列列宽 = 4
逻辑日空间第2列列宽 = 2.5
逻辑日空间第3列列宽 = 5
逻辑日空间第1行行高 = 20
逻辑日空间第2行行高 = 20
逻辑日空间第3行行高 = 20
逻辑日空间第4行行高 = 20
月份间隔行高 = 10
线宽 = 'thin'  # 线条宽度


def create_calendar(year=2025):
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = str(year)

    # 设置纸张大小为A4
    ws.page_setup.paperSize = ws.PAPERSIZE_A4

    # 设置纸张方向为横向
    ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE

    # 设置页边距，上下左右均为0.76cm (转换为英寸)
    margin_cm = 页边距
    margin_inch = margin_cm / 2.54  # 将厘米转换为英寸
    ws.page_margins = PageMargins(left=margin_inch, right=margin_inch, top=margin_inch, bottom=margin_inch)

    # 设置打印选项，使工作表打印在一页，有些版本没效果需要手工设置
    ws.print_options = PrintOptions(horizontalCentered=True, verticalCentered=True)
    ws.page_setup.fitToWidth = 1  # 将宽度调整以适应1页宽
    ws.page_setup.fitToHeight = 1  # 将高度调整以适应1页高
    # ws.page_setup.scale = 100  # 可选：设置缩放比例，默认是100%

    # 创建年份行
    年份行 = ws.row_dimensions[1]
    年份行.height = 年份行高
    total_columns = 31 * 3 + 1
    ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=total_columns)
    cell = ws.cell(row=1, column=1)
    cell.value = year
    cell.font = Font(name=年份字体, size=年份字号, bold=True)
    cell.alignment = Alignment(horizontal='center', vertical='center')

    row_start = 3  # 起始行号

    for month in range(1, 13):
        days_in_month = monthrange(year, month)[1]
        col_offset = 1  # 列偏移量

        # 月份标题
        ws.merge_cells(start_row=row_start, start_column=col_offset,
                       end_row=row_start + 3, end_column=col_offset)
        cell = ws.cell(row=row_start, column=col_offset)
        cell.value = f"{month}月"  # 月份加上修饰
        cell.font = Font(name=月份字体, size=月份字号, bold=True)
        cell.alignment = Alignment(horizontal='center', vertical='center')

        for day in range(1, days_in_month + 1):
            col_start = col_offset + (day - 1) * 3 + 1
            weekday = datetime(year, month, day).weekday()

            # 设置“逻辑日空间”的样式和内容
            for i in range(4):  # 遍历4行
                for j in range(3):  # 遍历3列
                    cell = ws.cell(row=row_start + i, column=col_start + j)
                    # “逻辑日空间”4行3列，第1行第1列 显示日期
                    if i == 0 and j == 0:
                        cell.value = day
                        cell.font = Font(name=日期字体, size=日期字号, bold=True)  # 增大日期字体
                        cell.alignment = Alignment(horizontal='center', vertical='center')  # 居中
                    elif j == 2 and i == 0: #第1行第3列 显示星期
                        cell.value = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'][weekday]
                        cell.font = Font(name=星期字体, size=星期字号)
                        cell.alignment = Alignment(horizontal='center', vertical='center')  # 居中
                    # 周五周六背景色不同
                    if weekday == 5:  # 星期六
                        cell.fill = PatternFill(start_color="E6FCC2", end_color="E6FCC2", fill_type="solid")
                    elif weekday == 6:  # 星期天
                        cell.fill = PatternFill(start_color="FFCCCC", end_color="FFCCCC", fill_type="solid")



            # 根据位置设置边框
            ws.cell(row=row_start, column=col_start).border = Border(left=Side(style=线宽), top=Side(style=线宽))
            ws.cell(row=row_start, column=col_start + 1).border = Border(top=Side(style=线宽))
            ws.cell(row=row_start, column=col_start + 2).border = Border(right=Side(style=线宽), top=Side(style=线宽))

            ws.cell(row=row_start + 1, column=col_start).border = Border(left=Side(style=线宽))
            ws.cell(row=row_start + 1, column=col_start + 2).border = Border(right=Side(style=线宽))

            ws.cell(row=row_start + 2, column=col_start).border = Border(left=Side(style=线宽))
            ws.cell(row=row_start + 2, column=col_start + 2).border = Border(right=Side(style=线宽))

            ws.cell(row=row_start + 3, column=col_start).border = Border(left=Side(style=线宽), bottom=Side(style=线宽))
            ws.cell(row=row_start + 3, column=col_start + 1).border = Border(bottom=Side(style=线宽))
            ws.cell(row=row_start + 3, column=col_start + 2).border = Border(right=Side(style=线宽),
                                                                             bottom=Side(style=线宽))

            # 设置列宽和行高
            ws.column_dimensions[openpyxl.utils.get_column_letter(col_start)].width = 逻辑日空间第1列列宽
            ws.column_dimensions[openpyxl.utils.get_column_letter(col_start + 1)].width = 逻辑日空间第2列列宽
            ws.column_dimensions[openpyxl.utils.get_column_letter(col_start + 2)].width = 逻辑日空间第3列列宽
            ws.row_dimensions[row_start].height = 逻辑日空间第1行行高
            ws.row_dimensions[row_start + 1].height = 逻辑日空间第2行行高
            ws.row_dimensions[row_start + 2].height = 逻辑日空间第3行行高
            ws.row_dimensions[row_start + 3].height = 逻辑日空间第4行行高

            # 合并第二、三、四行的三列
            for i in range(1, 4):
                ws.merge_cells(start_row=row_start + i, start_column=col_start,
                               end_row=row_start + i, end_column=col_start + 2)

        row_start += 5  # 空一行并为下一个月准备

    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    file_name = f"{year}年_{timestamp}.xlsx"
    wb.save(file_name)
    print(f"文件已保存为 {file_name}")

# 获取用户输入的年份
def get_year_input():
    year_input = input("请输入一个年份（直接回车默认2025年）：")
    return 2025 if year_input == "" else int(year_input)

if __name__ == "__main__":
    year = get_year_input()
    create_calendar(year)
